<?php

class HttpUtil {
	

	private static $_instance = null;

	public $_get_vars;
	public $_post_vars;
	public $_put_vars;
	public $_delete_vars;
	public $_session_vars;

	function __construct(){
			$this->_get_vars = (!empty($_GET)? $_GET: null) ;
			$this->_post_vars = (!empty($_POST)? $_POST: null);
			$this->_put_vars = (!empty($_PUT)? $_PUT: null);
			$this->_delete_vars = (!empty($_DELETE)? $_DELETE: null);
			$this->_session_vars = (!empty($_SESSION)? $_SESSION: null) ;
	}
	
	public static function getInstance() {
		if (self::$_instance == null) {
			self::$_instance = new HttpUtil();
		}
		return self::$_instance;
	}
	
	public function setGetVars( &$get_vars  ){
			$this->_get_vars = $get_vars ;
	}

	public function setPostVars( &$post_vars  ){
			$this->_post_vars = $post_vars ;
	}
	public function getPostVars(){
			return $this->_post_vars ;
	}

	public function setPutVars( &$put_vars  ){
			$this->_put_vars = $put_vars ;
	}
	public function getPutVars(){
			return $this->_put_vars ;
	}

	public function setDeleteVars( &$delete_vars  ){
			$this->_delete_vars = $delete_vars ;
	}
	public function getDeleteVars(){
			return $this->_delete_vars ;
	}


	public function setSessionVars( &$vars ){
			$this->_session_vars =&$vars ;
	}
	
	function array_key_exists_ic($key,$search) {
			if (array_key_exists($key,$search))
					return $key ;
			if (!(is_string($key) && is_array($search) && count($search)))
					return false ;
			foreach ($search as $k => $v)
					if (strcasecmp($k,$key) == 0) 
							return $k ;
			return false ;
	} 
	

	public function resolveParam( $name, $default, &$value ) {

			if( $this->_get_vars != null) {
					$varName = $this->array_key_exists_ic($name,$this->_get_vars);
					if ($varName !== false) {
							$value = $this->_get_vars[$varName] ;
							return true  ;
					}		
			}

			if( $this->_post_vars != null) {
					$varName = $this->array_key_exists_ic($name,$this->_post_vars);
					if ($varName !== false) {
							$value = $this->_post_vars[$varName] ;
							return true  ;
					}		
			}

			if( $this->_put_vars != null) {
					$varName = $this->array_key_exists_ic($name,$this->_put_vars);
					if ($varName !== false) {
							$value = $this->_put_vars[$varName] ;
							return true  ;
					}		
			}

			if( $this->_delete_vars != null) {
					$varName = $this->array_key_exists_ic($name,$this->_delete_vars);
					if ($varName !== false) {
							$value = $this->_delete_vars[$varName] ;
							return true  ;
					}		
			}


			if( $this->_session_vars != null) {
					$varName = $this->array_key_exists_ic($name,$this->_session_vars);
					if ($varName !== false) {
							$value = $this->_session_vars[$varName] ;
							return true  ;
					}		
			}

			if( $default == null )
					 return false ;

			$value = $default ;
			return true ;
	}

	public static function getMethod() {
		return $requestMethod = strtoupper($_SERVER['REQUEST_METHOD']);
	}
	
	public static function isPost() {
		return (self::getMethod() == 'POST');
	}
	
	public static function isGet() {
		return (self::getMethod() == 'GET');
	}
   
	public static function set201() {
		header("HTTP/1.1 201 Created");
		//TODO: set location
		// header('Location: http://www.foo.com/bar', true, 201);
	}

	public static function do400($error = 'error', $errorDescription = null) {
		$data = array('error' => $error, 'status' => 400);
		if (isset($errorDescription)) {
			$data['errorDescription'] = $errorDescription;
		}
		echo JsonRenderer::handle($data);
		header("HTTP/1.1 400 Bad request");
		exit();
	}
	
	public static function do401(){
		$data = array('status' => 401);
		echo JsonRenderer::handle($data);
		header('HTTP/1.1 401 Unauthorized');
		exit();
	}
	
	public static function do401Basic(){
		header('HTTP/1.1 401 Unauthorized');
		header('WWW-Authenticate: Basic realm="BRACO"');
		echo 'Erreur lors de l\'authentification';
		exit();
	}
	
	public static function do403(){
		$data = array('status' => 403);
		echo JsonRenderer::handle($data);
		header('HTTP/1.1 403 Forbidden');
		exit();
	}
	
	
	public static function do404(){
		$data = array('status' => 404);
		echo JsonRenderer::handle($data);
		header('HTTP/1.1 404 Not Found');
		exit();
	}
	
	
	public static function do405(){
		$data = array('status' => 405);
		echo JsonRenderer::handle($data);
		header('HTTP/1.1 405 Method Not Allowed ');
		exit();
	}
}